// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Persistor.java

package org.fiel.commons.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

// Referenced classes of package org.fiel.commons.jdbc:
//            Finder, ConnectionManager, Constraint

public abstract class Persistor
    implements Finder
{

    public Persistor()
    {
    }

    protected abstract String getTableNames();

    protected abstract Object extract(ResultSet resultset);

    protected abstract List getOrderByFields();

    public List FindAll(List constraints)
    {
        if(constraints != null)
            this.constraints = constraints;
        StringBuffer buffer = new StringBuffer();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList res = null;
        Object obj = null;
        boolean isFirst = true;
        appendFieldList(buffer);
        appendFromClause(buffer);
        appendConstraints(buffer);
        appendOrderByClause(buffer);
        try
        {
            conn = ConnectionManager.getInstance().getConnection();
            ps = conn.prepareStatement(buffer.toString());
            setearValores(ps);
            for(rs = ps.executeQuery(); rs.next(); res.add(obj))
            {
                if(isFirst)
                {
                    res = new ArrayList();
                    isFirst = false;
                }
                obj = extract(rs);
            }

        }
        catch(SQLException e)
        {
            logger.error("Error en Persistencia : " + e.getMessage());
            List list = null;
            return list;
        }
        finally
        {
            if(rs != null)
                try
                {
                    rs.close();
                }
                catch(Throwable throwable) { }
            if(ps != null)
                try
                {
                    ps.close();
                }
                catch(Throwable throwable1) { }
            if(conn != null)
                try
                {
                    conn.close();
                }
                catch(Throwable ignore) { }
        }
        return res;
    }

    public Object FindByPK(List constraints)
    {
        this.constraints = constraints;
        StringBuffer buffer = new StringBuffer();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Object res = null;
        appendFieldList(buffer);
        appendFromClause(buffer);
        appendConstraints(buffer);
        appendOrderByClause(buffer);
        try
        {
            conn = ConnectionManager.getInstance().getConnection();
            ps = conn.prepareStatement(buffer.toString());
            setearValores(ps);
            rs = ps.executeQuery();
            if(rs.next())
                res = extract(rs);
        }
        catch(SQLException e)
        {
            logger.error(e.getMessage());
            Object obj = null;
            return obj;
        }
        finally
        {
            if(rs != null)
                try
                {
                    rs.close();
                }
                catch(Throwable throwable) { }
            if(ps != null)
                try
                {
                    ps.close();
                }
                catch(Throwable throwable1) { }
            if(conn != null)
                try
                {
                    conn.close();
                }
                catch(Throwable ignore) { }
        }
        return res;
    }

    protected List getFields()
    {
        ArrayList ret = new ArrayList();
        ret.add("*");
        return ret;
    }

    protected void appendFieldList(StringBuffer buffer)
    {
        boolean isFirst = true;
        for(Iterator iter = getFields().iterator(); iter.hasNext();)
            if(isFirst)
            {
                buffer.append("select " + iter.next());
                isFirst = false;
            } else
            {
                buffer.append("," + iter.next());
            }

    }

    protected void appendFromClause(StringBuffer buffer)
    {
        buffer.append(" from " + getTableNames());
    }

    protected void appendConstraints(StringBuffer buffer)
    {
        if(constraints == null)
            return;
        boolean isFirst = true;
        for(Iterator iter = constraints.iterator(); iter.hasNext();)
            if(isFirst)
            {
                buffer.append(" where " + iter.next());
                isFirst = false;
            } else
            {
                buffer.append(" and " + iter.next());
            }

    }

    protected void appendOrderByClause(StringBuffer buffer)
    {
        List orderFields = getOrderByFields();
        if(orderFields == null)
            return;
        boolean isFirst = true;
        for(Iterator iter = orderFields.iterator(); iter.hasNext();)
            if(isFirst)
            {
                buffer.append(" order by " + iter.next());
                isFirst = false;
            } else
            {
                buffer.append("," + iter.next());
            }

    }

    protected void setearValores(PreparedStatement ps)
    {
        if(constraints == null)
            return;
        try
        {
            int i = 1;
            for(Iterator iter = constraints.iterator(); iter.hasNext();)
            {
                Constraint aConstraint = (Constraint)iter.next();
                Object valor = aConstraint.getValor();
                if(valor instanceof String)
                    ps.setString(i, (String)valor);
                else
                if(valor instanceof Integer)
                    ps.setInt(i, ((Integer)valor).intValue());
                else
                if(valor instanceof Date)
                    ps.setDate(i, new Date(((Date)valor).getTime()));
                i++;
            }

        }
        catch(Throwable throwable) { }
    }

    protected List constraints;
    public static Log logger = LogFactory.getLog("ar.com.incube.commons.jdbc");

}
